const Enroll = require('../models/Enroll')
const db = require('../db')
const _ = require('lodash')
const { debug } = require('../utils/constant')

//获取新闻列表
/* function NewsList() {
    return db.querySql(`select * from news order by id desc`)
} */
//获取对应分类的新闻列表
/* function NewsListCategory(id) {
    return db.querySql(`select * from news where columnid='${id}' order by id desc`)
} */
//获取新闻分类列表
/* function NewsCategoryList() {
    return db.querySql(`select * from category order by id desc`)
} */
async function NewsCategoryList() {
  const sql = 'select * from category order by id asc'
  const result = await db.querySql(sql)
  const categoryList = []
  result.forEach(item => {
    categoryList.push({
      label: item.categoryName,
      value: item.categoryName,
      num: item.id
    })
  })
  return categoryList
}
//获取单个报名信息
function getOneEnroll(id) {
    return db.queryOne(`select * from entry_form where id ='${id}' `)
}

//获取单个报名信息
function getOneExamine(id) {
  return db.queryOne(`select * from entry_form where enroll_adminuser ='${id}' `)
}
//插入报名信息
function insertEnroll(entry_form) {
    return new Promise(async (resolve, reject) => {
        try {
            if (entry_form) {
                await db.insert(entry_form, 'entry_form')//添加的表名
                resolve()

            } else {
                reject(new Error('添加的信息不正确'))
            }
        } catch (e) {
            reject(e)
        }
    })
}

//插入审核信息
function insertEnrollExamine(entry_form) {
  return new Promise(async (resolve, reject) => {
      try {
          if (entry_form) {
              await db.insert(entry_form, 'examine')//添加的表名
              resolve()
          } else {
              reject(new Error('添加的信息不正确'))
          }
      } catch (e) {
          reject(e)
      }
  })
}
//更新报名信息
function updateEnroll(Enroll) {
    return new Promise(async (resolve, reject) => {
        try {
            if (Enroll) {
                await db.update(Enroll, 'entry_form' ,`where id='${Enroll.id}'`)//添加的表名
                resolve()
            } else {
                reject(new Error('修改的报名信息不正确'))
            }
        } catch (e) {
            reject(e)
        }
    })
}

//删除报名信息
function deleteEnroll(newsId) {
    return new Promise(async (resolve, reject) => {
        if(newsId){
            const sql = `delete from entry_form where id='${newsId}'`
            db.querySql(sql).then(() => {
                resolve()
            })}
        else {
            reject(new Error('删除报名信息失败'))
        }
    })
}

async function EnrollList(query) {
    debug && console.log(query)
    const {
      enroll_name,
      enroll_status,
      enroll_adminuser,
      Current_Registered_Household,
      post_code1,
      sort,
      page = 1,
      pageSize = 20
    } = query
    const offset = (page - 1) * pageSize
    let newsSql = 'select * from entry_form'
    let where = 'where'
    enroll_name && (where = db.andLike(where, 'enroll_name', enroll_name))
    enroll_status && (where = db.and(where, 'enroll_status', enroll_status))
    enroll_adminuser && (where = db.and(where, 'enroll_adminuser', enroll_adminuser))
    Current_Registered_Household && (where = db.andLike(where, 'Current_Registered_Household', Current_Registered_Household))
    post_code1 && (where = db.and(where, 'post_code', post_code1))
    if (where !== 'where') {
      newsSql = `${newsSql} ${where}`
    }
    if (sort) {
      const symbol = sort[0]
      const column = sort.slice(1, sort.length)
      const order = symbol === '+' ? 'desc' : 'asc'
      newsSql = `${newsSql} order by \`${column}\` ${order}`
    }
    let countSql = `select count(*) as count from entry_form`
    if (where !== 'where') {
      countSql = `${countSql} ${where}`
    }
    const count = await db.querySql(countSql)
    newsSql = `${newsSql} limit ${pageSize} offset ${offset}`
    const list = await db.querySql(newsSql)
    //list.forEach(news => news.cover = Book.genCoverUrl(book))
    return { list, count: count[0].count, page, pageSize }
  }

  async function ExamineList(query) {
    debug && console.log(query)
    const {
      enroll_adminuser
    } = query
    let newsSql = 'select * from examine'
    let where = 'where'
    enroll_adminuser && (where = db.and(where, 'examine_name', enroll_adminuser))
    if (where !== 'where') {
      newsSql = `${newsSql} ${where} order by id desc limit 1`
    }
    const list = await db.querySql(newsSql)
    //list.forEach(news => news.cover = Book.genCoverUrl(book))
    return list 
  }

  async function listExamineEnroll(query) {
    debug && console.log(query)
    const {
      enroll_adminuser
    } = query
    let newsSql = 'select * from examine'
    let where = 'where'
    enroll_adminuser && (where = db.and(where, 'examine_name', enroll_adminuser))
    if (where !== 'where') {
      newsSql = `${newsSql} ${where} order by id desc`
    }
    const list = await db.querySql(newsSql)
    //list.forEach(news => news.cover = Book.genCoverUrl(book))
    return list 
  }

  async function UserEnrollList(query) {
    debug && console.log(query)
    const {
      enroll_name,
      enroll_status,
      enroll_adminuser,
      sort,
      page = 1,
      pageSize = 20
    } = query
    const offset = (page - 1) * pageSize
    let newsSql = 'select * from entry_form'
    let where = 'where'
    enroll_name && (where = db.andLike(where, 'enroll_name', enroll_name))
    enroll_status && (where = db.and(where, 'enroll_status', enroll_status))
    enroll_adminuser && (where = db.and(where, 'enroll_adminuser', enroll_adminuser))
    if (where !== 'where') {
      newsSql = `${newsSql} ${where}`
    }
    if (sort) {
      const symbol = sort[0]
      const column = sort.slice(1, sort.length)
      const order = symbol === '+' ? 'asc' : 'desc'
      newsSql = `${newsSql} order by \`${column}\` ${order}`
    }
    let countSql = `select count(*) as count from entry_form`
    if (where !== 'where') {
      countSql = `${countSql} ${where}`
    }
    const count = await db.querySql(countSql)
    newsSql = `${newsSql} limit ${pageSize} offset ${offset}`
    const list = await db.querySql(newsSql)
    //list.forEach(news => news.cover = Book.genCoverUrl(book))
    return { list, count: count[0].count, page, pageSize }
  }
 /*  function deleteBook(fileName) {
    return new Promise(async (resolve, reject) => {
      let book = await getBook(fileName)
      if (book) {
        if (+book.updateType === 0) {
          reject(new Error('内置电子书不能删除'))
        } else {
          const bookObj = new Book(null, book)
          const sql = `delete from book where fileName='${fileName}'`
          db.querySql(sql).then(() => {
            bookObj.reset()
            resolve()
          })
        }
      } else {
        reject(new Error('电子书不存在'))
      }
    })
  } */

module.exports = {
    insertEnroll,
    updateEnroll,
    deleteEnroll,
    EnrollList,
    getOneEnroll,
    UserEnrollList,
    insertEnrollExamine,
    ExamineList,
    getOneExamine,
    listExamineEnroll
}
